package com.zz.ecommerce;

import com.zz.ecommerce.user.UserDao;
import com.zz.ecommerce.user.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * 用户登录controller
 *
 * @author
 */
@Controller
@RequestMapping("/security")
public class LoginController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserDao userDao;
    @Value("${trans.manager.url}")
    private String transManagerUrl;

    @RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/icLogin", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String icLogin() {
        return "icLogin";
    }

    @RequestMapping(value = "/icLogin2", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String icLogin2() {
        return "icLogin2";
    }

    @RequestMapping(value = "/icLoginBiz", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String icLoginBiz() {
        return "icLoginBiz";
    }

    @RequestMapping(value = "/logout", method = {RequestMethod.GET, RequestMethod.POST})
    public String logout() {
        try {
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            logger.error("", e);
        }
        return "login";
    }

    @RequestMapping(value = "doLogin", method = RequestMethod.POST)
    public String doLogin(HttpSession session, String txtUserName, String passUserPassword, ModelMap modelMap) {
        logger.info("txtUserName=============" + txtUserName);
        logger.info("passUserPassword=============" + passUserPassword);
        UserInfo userVo = new UserInfo();
//        userDao.getUserById(txtUserName, MD5Encrypt.getMD5(passUserPassword));
//        if (null == userVo) {
//            model.addAttribute("message", "用户名密码错误!");
//            return "login";
//        } else {
        userVo.setCompany("山东海运船舶管理有限公司");
        userVo.setCompanyId("1818");
        userVo.setCompanyName("山东海运船舶管理有限公司");
        userVo.setDepartmentId("00001");
        userVo.setName("lilin");
        userVo.setLogName("lilin");
        userVo.setOrganizationId("98");
        session.setAttribute("UserInfo", userVo);
//        }
        modelMap.put("transManagerUrl", transManagerUrl);
        return "portal";
    }

    @RequestMapping(value = "/editPasswd", method = RequestMethod.POST)
    public String editPasswd(HttpSession session, Model model) {

        UserInfo userVo = (UserInfo) session.getAttribute("UserInfo");
        if (null == userVo) {
            model.addAttribute("message", "用户不存在请重新登录!");
            return "login";
        }

        logger.info(">>>>>>>>>>>>username is:[" + userVo.getLogName() + "]");
        logger.info(">>>>>>>>>>>>password is:[" + userVo.getLogPassword() + "]");

        UserInfo user = userDao.getUserById(userVo.getLogName(), userVo.getLogPassword());
        if (null == userVo) {
            model.addAttribute("message", "用户不存在请重新登录!");
            return "login";
        } else {
            session.setAttribute("UserInfo", userVo);
        }
        return "redirect:/user/changePassword";
    }

}
